<?php

namespace app\api\controller;
use app\common\business\User as UserBis;
class User extends AuthBase
{
    public function index()
    {
       $user = (new UserBis())->getNormalUserById($this->userId);
       $resultUser = [
           "id" => $this->userId,
           "username" => $user['username'],
           "sex" => $user['sex']
       ];
       return show(config("status.success"),"ok",$resultUser);
    }

    public function update()
    {
        $username = $this->request->param("username","","trim");
        $sex = $this->request->param("sex",0,"intval");

        $data = [
            "username" => $username,
            "sex" => $sex
        ];

        $validate = (new \app\api\validate\User())->scene("update_user");
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }

        $userBisObj = new UserBis();
        $user = $userBisObj->update($this->userId,$data);

        if(!$user){
            return show(config("status.error"),'更新失败');
        }


//        $redisUser = json_encode(['id' => $this->userId,'username' => $data['username']]);
//        cache(config("redis.token_pre").$this->accessToken,$redisUser);

        return show(config("status.success"),"更新成功");
    }
}